﻿using Pay.DTO;
using Pay.DTO.Token;
using Pay.IService;
using Pay.TO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;

namespace Pay.Token.Controllers {
    /// <summary>
    /// 网申网贷H5
    /// </summary>
    [AppAuthorize("Member")]
    public class CardController : GeneralController {
        private ITokenCardService _iCardService;
        private IMerchantService _iMerchantService;
        private ISettingService _iSettingService;
        private ISoupTaskService _iSoupTaskService;

        /// <summary>
		/// 实例化
		/// </summary>
		public CardController(ITokenCardService iCardService, IMerchantService iMerchantService,
                              ISettingService iSettingService, ISoupTaskService iSoupTaskService) {
            _iCardService = iCardService;
            _iMerchantService = iMerchantService;
            _iSettingService = iSettingService;
            _iSoupTaskService = iSoupTaskService;
        }

        /// <summary>
        /// 快速办卡
        /// </summary>
        /// <returns>The coin.</returns>
        [HttpGet]
        public async Task<ActionResult> FastCard() {
            ViewBag.banks = await _iCardService.GetBank();
            ViewBag.topics = await _iCardService.GetBankTopic();
            ViewBag.tags = await _iCardService.GetBankTag();
            return View();
        }

        /// <summary>
        /// 获取办卡列表
        /// </summary>
        /// <returns>The grade.</returns>
        /// <param name="query">Model.</param>
        [HttpPost]
        public async Task<PartialViewResult> _FastCard(DTO.Token.card_req query) {
            PagedResult<DTO.Token.bank_card_resp> result = await _iCardService.GetBankCard(query);
            return PartialView(result);
        }

        /// <summary>
        /// 卡片详情
        /// </summary>
        /// <returns>The coin.</returns>
        [HttpGet]
        public async Task<ActionResult> CardDetail(long id) {
            ViewBag.BankCardChannelId = id;
            var result = await _iCardService.GetBankCardDetail(id);
            return View(result);
        }

        /// <summary>
        ///  申请人信息
        /// </summary>
        /// <returns>The coin.</returns>
        [HttpGet]
        public ActionResult ProposerInfo(long id) {
            ViewBag.BankCardChannelId = id;
            return View();
        }

        /// <summary>
        ///  保存申请人信息
        /// </summary>
        /// <returns>The coin.</returns>
        [HttpPost]
        public async Task<ActionResult> SaveProposerInfo(DTO.Token.card_save_model model) {
            model.bind_no = _iMerchantService.Get(UserId).merchant_no;
            model.order_no = "O" + DateTime.Now.ToString("yyyyMMddHHmmssffff") + DtoHelper.RandCode(6);
            model.notify = "";
            var result = await _iCardService.Post(model);
            var cardApp = _iSoupTaskService.TaskRecive(UserId, ETaskType.CreditCardApplication).Result;
            //if (result.url == null || result.url == "")
            //    return Json(ResultToJson.Json(500, "申请失败", ""));
            //else
            return Json(ResultToJson.Json("申请成功"));
        }

        /// <summary>
        ///  保存申请人信息
        /// </summary>
        /// <returns>The coin.</returns>
        [HttpGet]
        public async Task<ActionResult> CardProgress() {
            ViewBag.banks = await _iCardService.GetBank();
            return View();
        }
    }
}